/*
 * 软件版权: 恒生电子股份有限公司
 * 修改记录:
 * 修改日期     修改人员  修改说明
 * @Version:1.0
 * ========    =======  ============================================
 * 2025/3/8  hspcadmin  com.msb.mall.product.exception 新增
 * ========    =======  ============================================
 */
package com.msb.common.exception;

import com.msb.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能说明:
 *
 * @author hspcadmin
 */
@Slf4j
@RestControllerAdvice(basePackages = "com.msb.mall.product.controller")
public class ExceptionControllerAdvice {
  @ExceptionHandler(value = MethodArgumentNotValidException.class)
  public R handlerValidException(MethodArgumentNotValidException e){
    Map<String,String> map = new HashMap<>();
    e.getFieldErrors().forEach((fieldError) -> {
      map.put(fieldError.getField(), fieldError.getDefaultMessage());
    });
    return R.error(BizCodeEnum.VALID_EXCEPTION.getCode(),BizCodeEnum.VALID_EXCEPTION.getMsg()).put("data",map);
  }

  @ExceptionHandler(Throwable.class)
  public R handleException(Throwable throwable){
    log.error("错误信息:", throwable);
    return R.error(BizCodeEnum.UNKOWN_EXCEPTION.getCode(), BizCodeEnum.UNKOWN_EXCEPTION.getMsg()).put("data", throwable.getMessage());
  }
}
